<?php
namespace Home\Controller;
use Common\Controller\CommonController;

class CategoryController extends CommonController {

	function index() {
		$id=I('id');
		
		$where=array('cid'=>$id,'pid'=>0);
		//查出一级分类
		$res = M('Category') -> where($where) -> select();
		//多少个一级分类
		$cnt=count($res);
		//循环出二级分类
		 for($i = 0 ;$i < $cnt ; $i++){
            $two_id = $res[$i]['id'];
            $re = M('category')->where("pid = ".$two_id)->select();
            $res[$i]['son'] = $re;
		}
		$list = array();
		foreach ($res as $key => $value) {
			$value['cate_img'] = json_decode($value['cate_img'], true);
			$value['cate_img']=$value['cate_img'][0];
			$list[] = $value;
		}
		//P($list);
		$this -> assign('id',$id);
		$this -> assign('list', $list);
		$this -> display();
	}
	
	
	function bbs() {
		
		
		$where=array('cid'=>1,'pid'=>0);
		//查出一级分类
		$res = M('Category') -> where($where) -> select();
		//多少个一级分类
		
		$list = array();
		foreach ($res as $key => $value) {
			$value['cate_img'] = json_decode($value['cate_img'], true);
			$value['cate_img']=$value['cate_img'][0];
			$list[] = $value;
		}
		//P($list);
		$this -> assign('id',$id);
		$this -> assign('list', $list);
		$this -> display();
	}
	
	//修改二级分类
	public function update_two(){
			$id=I('id');
			$list=M('category')->find($id);
			if($list){
				$this->ajaxReturn(array('s'=>true,'d'=>$list));
			}else{
				$this->ajaxReturn(array('s'=>false));
			}
	}

	public function update(){
		$id=I('id');
		$category = I('category');
		$systems = I('systems');

		$data = array('id'=>$id,'category'=>$category, 'systems'=>$systems);

		$list=M('category')->data($data)->save();

		if($list){
				$this->ajaxReturn(array('s'=>true,'m'=>'修改成功!'));
			}else{
				$this->ajaxReturn(array('s'=>false,'m'=>'修改失败!'));
			}
	}


	//添加二级分类
	public function add_two(){
		$category=I('category');
		$systems = I('systems');
		$pid=I('pid');
		$data = array(
			'category'=>$category,
			'systems'=> $systems,
			'cid'=>3,
			'pid'=>$pid,
		);
		$list=M('category')->data($data)->add();

		if($list){
			$this->ajaxReturn(array('s'=>true,'m'=>"添加成功!"));
		}else{
			$this->ajaxReturn(array('s'=>false,'m'=>"添加失败!"));
		}
	}





	//删除二级分类
	public function del_two(){
		$id = I('id');
		$list=M('category')->where(array('id'=>$id))->delete();
		if($list){
			$this->ajaxReturn(array('s'=>true,'m'=>"删除成功!"));
		}else{
			$this->ajaxReturn(array('s'=>false,'m'=>"删除失败!"));
		}
	}


		//添加板块
	 function add_category() {
	 	
	 	$category=I('category');
	 	$cate_img=json_encode(I('cate_img'));
	 	$data=array('cid'=>1,'pid'=>0,'category'=>$category,'cate_img'=>$cate_img);
	 	
	 	$res=M('Category')->add($data);
	 	if ($res) {
	 		$this -> ajaxReturn(array('s' => true, 'm' => '添加成功'));
	 	} else {
	 		$this -> ajaxReturn(array('s' => false, 'm' => '添加失败'));
	 	}		
	 }
	
	 function up_category() {
	 	$id=I('id');
		$list=M('Category')->where(array('id'=>$id))->find();
		$list['cate_img']=json_decode($list['cate_img'],true);
	 	if ($list) {
	 		$this -> ajaxReturn(array('s' => true, 'd' => $list));
	 	} else {
	 		$this -> ajaxReturn(array('s' => false));
	 	}		
	 }
	 
	 
	  function upp_category() {
	 	$id=I('id');
		$category=I('category');
	 	$cate_img=json_encode(I('cate_img'));
	 	$data=array('cid'=>1,'pid'=>0,'category'=>$category,'cate_img'=>$cate_img);
	 	$where['id']=$id;
	 	$res=M('Category')->where($where)->save($data);
	 	if ($res) {
	 		$this -> ajaxReturn(array('s' => true, 'm' => '修改成功'));
	 	} else {
	 		$this -> ajaxReturn(array('s' => false, 'm' => '修改失败'));
	 	}		
	 }
	 function del_category() {
	 	$cid=I('cid');
	 	$where['id'] = I('id');
	 	$res = M('Category') -> where($where) -> delete();
	 	if ($res) {
	 		$this -> ajaxReturn(array('s' => true, ));
	 	} else {
	 		$this -> ajaxReturn(array('s' => false, ));
	 	}	
	 }
	
}
